package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.marketing.MarketingPosterCreateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingPosterUpdateDTO;
import com.pactera.madp.cp.api.entity.marketing.MarketingPosterEntity;
import com.pactera.madp.cp.api.vo.marketing.MarketingPosterVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.marketing.MarketingPosterMapper;
import com.pactera.madp.cp.service.MarketingHotService;
import com.pactera.madp.cp.service.MarketingPosterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (McMarketingPoster)表服务实现类
 *
 * @author yushen.qi
 * @since 2021-05-24 18:55:14
 */
@Service
public class MarketingPosterServiceImpl extends ServiceImpl<MarketingPosterMapper, MarketingPosterEntity> implements MarketingPosterService {

    @Resource
    private MarketingHotService marketingHotService;

    @Override
    public List<MarketingPosterVO> selectAllOrName(Integer classification) {
        int corpId = CorpContextHolder.getCorpId();
        return list(Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getCorpId, corpId).eq(MarketingPosterEntity::getClassification,classification)).stream().map(entity ->
        {
            MarketingPosterVO MarketingPosterVO = new MarketingPosterVO();
            BeanUtil.copyProperties(entity, MarketingPosterVO);
            return MarketingPosterVO;
        }).collect(Collectors.toList());
    }

    @Override
    public IPage selectAllByPageOrName(Page page, Integer classification,String title) {
        Integer corpId = CorpContextHolder.getCorpId();
        QueryWrapper<MarketingPosterEntity> wrapper = new QueryWrapper<>();
        if(null!=title){
            wrapper.eq("corp_id",corpId).eq("marketing_state",2).like("title",title).eq("classification",classification);
        }else {
            wrapper.eq("corp_id",corpId).eq("marketing_state",2).eq("classification",classification);
        }
            IPage iPage = baseMapper.selectPage(page, wrapper);
            Object collect = iPage.getRecords().stream().map(entity -> {
                MarketingPosterVO marketingPosterVO = new MarketingPosterVO();
                BeanUtil.copyProperties(entity, marketingPosterVO);
                return marketingPosterVO;
            }).collect(Collectors.toList());
            iPage.setRecords((List) collect);
            return iPage;
    }

    @Override
    public Map<String, Integer> insert(MarketingPosterCreateDTO dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        int tenantId = AccountService.getTenantId();
        MarketingPosterEntity posterEntity = new MarketingPosterEntity();
        BeanUtil.copyProperties(dto,posterEntity);
      //  posterEntity.setTenantId(tenantId);
        posterEntity.setCreateTime(LocalDateTime.now());
        posterEntity.setUpdateTime(LocalDateTime.now());
        posterEntity.setCorpId(corpId);
        posterEntity.setMarketingState(1);
        posterEntity.setHotUuid("a");
        save(posterEntity);
        MarketingPosterEntity one = getOne(Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getCorpId, corpId).orderByDesc(MarketingPosterEntity::getId).last("limit 1"));
        HashMap<String, Integer> hashMap = new HashMap<>(1);
        hashMap.put("id",one.getId());
        return hashMap;

    }

    @Override
    public boolean update(MarketingPosterUpdateDTO marketingPosterUpdateDto) {
        MarketingPosterEntity posterEntity = new MarketingPosterEntity();
        BeanUtil.copyProperties(marketingPosterUpdateDto,posterEntity);
        posterEntity.setUpdateTime(LocalDateTime.now());
        posterEntity.setMarketingState(3);
        posterEntity.setHotUuid(null);
        return update(posterEntity, Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getId,posterEntity.getId()));
    }

    @Override
    public R<Boolean> remove(Integer id) {
        MarketingPosterEntity one = getOne(Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getId, id));
        if (one.getMarketingState()==2){
            return R.failed("该素材已被发布，无法删除！");
        }
        if(one.getRecState()==1){
            return R.failed("该素材已被推荐，无法删除！");
        }

//        MarketingHotEntity hotEntity = marketingHotService.getOne(Wrappers.<MarketingHotEntity>lambdaQuery()
//                .eq(MarketingHotEntity::getCorpId,CorpContextHolder.getCorpId())
//                .orderByDesc(MarketingHotEntity::getId).last("limit 1"));
//        if(null!=hotEntity){
//            if(one.getHotUuid().equals(hotEntity.getHotUuid())){
//                return R.failed("该素材已在热榜发布，无法删除！");
//            }
//        }
        return remove(Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getId,id))?R.ok():R.failed("删除失败！");
    }


}
